﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EHTWebsite.Code;
/// <summary>
/// Author  toanvt
/// CreatedDate 7/12/2011
/// </summary>
public partial class Administrator_AdminMapIU : System.Web.UI.Page
{

    public Int32 ID = -1;
    private String message = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Message"] != null)
            {
                try
                {
                    message = Request.QueryString["Message"].ToString();
                }
                catch (Exception)
                {
                    message = "";
                }
            }

            //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
            this.lbMessage.Text = message;
            this.lbMessage.CssClass = "BlueCorlor";

            try
            {
                ID = Int32.Parse(Request.QueryString["ID"].ToString());
            }
            catch
            {
                ID = -1;
            }

            if (ID == -1)
            {
                this.lbAdminBanDo.Text = "Thêm mới tọa độ";
                ViewState["action"] = HangSo.ACTION_ADD;
            }
            else
            {
                this.lbAdminBanDo.Text = "Sửa tọa độ";
                EHTWebsite.Code.BanDo theBanDo = BanDoProvider.GetBanDoByID(ID);

                if (theBanDo != null)
                {
                    this.txtToaDo.Text = theBanDo.ToaDo;
                    this.fckMoTaOther.Value = theBanDo.MoTaOther;
                    this.fckMoTaVN.Value = theBanDo.MoTaVN;
                    this.ckPublish.Checked = theBanDo.Publish;
                    this.txtNumberOfOrder.Text = theBanDo.NumberOfOrder.ToString();

                    ViewState["ID"] = theBanDo.ID;
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            EHTWebsite.Code.BanDo theBanDo = new EHTWebsite.Code.BanDo();
            Int32 banDoIDOut = -1;
            theBanDo.MoTaOther = this.fckMoTaOther.Value;
            theBanDo.MoTaVN = this.fckMoTaVN.Value;
            theBanDo.ToaDo = this.txtToaDo.Text.Trim();
            theBanDo.Publish = this.ckPublish.Checked;

            try
            {
                theBanDo.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theBanDo.NumberOfOrder = 1;
            }

            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theBanDo.ID = -1;
                banDoIDOut = BanDoProvider.IUBanDo(theBanDo, true);
            }
            else
            {
                theBanDo.ID = Int32.Parse(ViewState["ID"].ToString());
                banDoIDOut = BanDoProvider.IUBanDo(theBanDo, false);
            }

            Response.Redirect("AdminMaps.aspx");
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminMaps.aspx");
    }

    protected void btnApply_Click(object sender, EventArgs e)
    {
        Page.Validate();

        if (Page.IsValid)
        {
            EHTWebsite.Code.BanDo theBanDo = new EHTWebsite.Code.BanDo();
            Int32 banDoIDOut = -1;
            theBanDo.MoTaOther = this.fckMoTaOther.Value;
            theBanDo.MoTaVN = this.fckMoTaVN.Value;
            theBanDo.ToaDo = this.txtToaDo.Text.Trim();
            theBanDo.Publish = this.ckPublish.Checked;

            try
            {
                theBanDo.NumberOfOrder = Int32.Parse(this.txtNumberOfOrder.Text.Trim());
            }
            catch (Exception ex)
            {
                theBanDo.NumberOfOrder = 1;
            }

            String action = ViewState["action"].ToString();

            if (action == HangSo.ACTION_ADD)
            {
                theBanDo.ID = -1;
                banDoIDOut = BanDoProvider.IUBanDo(theBanDo, true);
            }
            else
            {
                theBanDo.ID = Int32.Parse(ViewState["ID"].ToString());
                banDoIDOut = BanDoProvider.IUBanDo(theBanDo, false);
            }

            Response.Redirect("AdminMapIU.aspx?ID=" + banDoIDOut.ToString() + "&Message=Lưu thành công");
        }
    }
}
